//
//  MDView.m
//  MaterialDesign
//
//  Created by Andrew Kopanev on 1/15/15.
//  Copyright (c) 2015 Moqod. All rights reserved.
//

#import "MDViewOne.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "UIView+MaterialDesign.h"

const NSTimeInterval MDViewOneAnimationDuration		= 0.5;

@interface MDViewOne (){
     NSMutableArray *soList;
}

@property (nonatomic, readonly) UIView			*subview;
@property (nonatomic, readonly) UISwitch		*subviewSwitch;
@property (nonatomic, readonly) UIButton		*subviewButton;

@property (nonatomic, strong) NSMutableArray	*controls;

@end

@implementation MDViewOne

#pragma mark - helpers

- (CGFloat)randomNumber0_1 {
	return ( rand() % 10000 ) / 10000.0;
}

- (UIColor *)randomColor {
	return [UIColor colorWithRed:[self randomNumber0_1] green:[self randomNumber0_1] blue:[self randomNumber0_1] alpha:1.0];
}

#pragma mark - initialization

- (instancetype)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
        
        soList=[NSMutableArray array];
        [self refreshListData];
        
        //NSLog(@"这是：%li",[soList count]);
		
		srand( time(0) );
		
//		self.backgroundColor = [UIColor whiteColor];
//		self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//		
//		_subview = [UIView new];
//		self.subview.backgroundColor = [UIColor colorWithRed:55.0 / 255.0 green:64.0 / 255.0 blue:69.0 / 255.0 alpha:1.0];
//		[self addSubview:self.subview];
//		
//		UILabel *subviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 1.0, 1.0)];
//        subviewLabel.userInteractionEnabled=YES;
//        subviewLabel.textAlignment = NSTextAlignmentLeft;
//		subviewLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
//		subviewLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//		subviewLabel.numberOfLines = 0;
//		subviewLabel.text = @"本日订单数量汇总：500，金额9000\n本周订单数量汇总：3400，金额：56120\n本月订单数量：73221，金额：1562532";
//		subviewLabel.textColor = [UIColor whiteColor];
//        UITapGestureRecognizer *tapGestureTel=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
//        [subviewLabel addGestureRecognizer:tapGestureTel];
//		[self.subview addSubview:subviewLabel];
//		
//
//		
//		self.controls = [NSMutableArray new];
//		const NSInteger controlsCount = 3;
//		for (NSInteger i = 0; i < controlsCount; i++) {
//			UIControl *control = [UIControl new];
//			control.backgroundColor = [self randomColor];
//			[control addTarget:self action:@selector(controlAction:event:) forControlEvents:UIControlEventTouchUpInside];
//            UILabel *lbshow=[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 1.0, 1.0)];
//            lbshow.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//            lbshow.numberOfLines = 0;
//            lbshow.textAlignment = NSTextAlignmentJustified;
//            
//            lbshow.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
//            if(i==0){
//                lbshow.text=@"报价单待审核：3\n订单待审核：5 \n生产单待审核：2";
//            }
//            else{
//                lbshow.text=@"建设中...";
//            }
//            lbshow.textColor = [UIColor whiteColor];
//            [control addSubview:lbshow];
//			[self addSubview:control];
//            [self.controls addObject:control];
//		}
		

	}
	return self;
}


-(void)createCirle{
    
        
    self.backgroundColor = [UIColor whiteColor];
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    _subview = [UIView new];
    self.subview.backgroundColor = [UIColor colorWithRed:55.0 / 255.0 green:64.0 / 255.0 blue:69.0 / 255.0 alpha:1.0];
    [self addSubview:self.subview];
    
    UILabel *subviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 1.0, 1.0)];
    subviewLabel.userInteractionEnabled=YES;
    subviewLabel.textAlignment = NSTextAlignmentLeft;
    subviewLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
    subviewLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    subviewLabel.numberOfLines = 0;
    if([soList count]>0){
       NSDictionary * dict = [soList objectAtIndex:0];
       NSString *ltext= [dict objectForKey:@"Context"] ;
       NSArray * ltextArr=[ltext componentsSeparatedByString:NSLocalizedString(@"_", nil)];
       NSString *ltext2=@"";
        for(int j=0;j<[ltextArr count];j++){
            if(![ltext2 isEqualToString:@""]){
                ltext2=[[NSString alloc]initWithFormat:@"%@\n",ltext2];
            }
            ltext2=[[NSString alloc]initWithFormat:@"%@%@",ltext2,[ltextArr objectAtIndex:j]];
        }
        subviewLabel.text=ltext2;

    }
    else{
        subviewLabel.text=@"";
    }
    subviewLabel.textColor = [UIColor whiteColor];
    UITapGestureRecognizer *tapGestureTel=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
    [subviewLabel addGestureRecognizer:tapGestureTel];
    [self.subview addSubview:subviewLabel];
    
    
    self.controls = [NSMutableArray new];
    const NSInteger controlsCount = 3;
    for (NSInteger i = 0; i < controlsCount; i++) {
        UIControl *control = [UIControl new];
        control.backgroundColor = [self randomColor];
        [control addTarget:self action:@selector(controlAction:event:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *lbshow=[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 1.0, 1.0)];
        lbshow.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        lbshow.numberOfLines = 0;
        lbshow.textAlignment = NSTextAlignmentJustified;
        
        lbshow.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
        if([soList count]>i+1){
            NSDictionary * dict = [soList objectAtIndex:i+1];
            NSString *ltext= [dict objectForKey:@"Context"] ;
            NSArray * ltextArr=[ltext componentsSeparatedByString:NSLocalizedString(@"_", nil)];
            NSString *ltext2=@"";
            for(int j=0;j<[ltextArr count];j++){
                if(![ltext2 isEqualToString:@""]){
                    ltext2=[[NSString alloc]initWithFormat:@"%@\n",ltext2];
                }
                ltext2=[[NSString alloc]initWithFormat:@"%@%@",ltext2,[ltextArr objectAtIndex:j]];
            }
            lbshow.text=ltext2;
            
            
            
        }
        else{
            lbshow.text=@"建设中...";
        }
        
        lbshow.textColor = [UIColor whiteColor];
        [control addSubview:lbshow];
        [self addSubview:control];
        [self.controls addObject:control];
    }
}



//刷新数据
-(void)refreshListData
{
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    NSDictionary *dict = [[NSDictionary alloc]init];
    [htnet hmNetWCFmethodName:@"srvmainform/QryForm" andDic:dict complete:^(NSString *backstr)
     {
         NSLog(@"33");
         NSDictionary *rootDict=[HTFrameNet hmConvertDictFromJsonStr:backstr];
         NSMutableArray *resultdt=[rootDict objectForKey:kpNET_RETURN_ENTITY_DATA];//[HTFrameNet hmConvertArrayFromJsonStr:backstr];
         
         for (int i=0; i<[resultdt count]; i++) {
             [soList addObject:[resultdt objectAtIndex:i]];
             
         }
         NSLog(@"haha:%li",[soList count]);
         [self createCirle];
         
         
         
     }];
    
//    if([soList count]==0){
//        [self refreshListData];
//    }
    
    
    
}

#pragma mark - actions

-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
//    NSLog(@"1121111");
//    UIView *next = [self subview];
//    
//    UIResponder *nextResponder = [next nextResponder];
//    
//
//        UIViewController *vc=(UIViewController *)nextResponder;
//        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"So" bundle:nil];
//        
//        UIViewController *test2obj=[secondStroyBoard instantiateViewControllerWithIdentifier:@"so"];
//        
//        [vc.navigationController pushViewController:test2obj animated:YES];

        
    
    
    
    //[self prese]

    
    
    
}

- (void)buttonAction:(UIControl *)sender event:(UIEvent *)event {
	CGPoint position = [[[event allTouches] anyObject] locationInView:self.subview];
	[self.subview mdInflateAnimatedFromPoint:position backgroundColor:[self randomColor] duration:MDViewOneAnimationDuration completion:nil];
}

- (void)controlAction:(UIControl *)sender event:(UIEvent *)event {
	CGPoint position = [[[event allTouches] anyObject] locationInView:sender];
	
	NSInteger index = [self.controls indexOfObjectIdenticalTo:sender];
	if (index % 2) {
		[sender mdInflateAnimatedFromPoint:position backgroundColor:[self randomColor] duration:MDViewOneAnimationDuration completion:nil];
	} else {
		[sender mdDeflateAnimatedToPoint:position backgroundColor:[self randomColor] duration:MDViewOneAnimationDuration completion:nil];
	}
}

- (void)subviewSwitchAction:(UISwitch *)swtch {
	if (swtch.isOn) {
		[self.subview mdInflateAnimatedFromPoint:swtch.center backgroundColor:[UIColor colorWithRed:0.0 / 255.0 green:150.0 / 255.0 blue:137.0 / 255.0 alpha:1.0] duration:MDViewOneAnimationDuration completion:nil];
	} else {
		[self.subview mdDeflateAnimatedToPoint:swtch.center backgroundColor:[UIColor colorWithRed:55.0 / 255.0 green:64.0 / 255.0 blue:69.0 / 255.0 alpha:1.0] duration:MDViewOneAnimationDuration completion:nil];
	}
}

#pragma mark - layout

- (void)layoutSubviews {
	[super layoutSubviews];

	CGFloat detailsButtonHeight = 60;
	CGFloat subviewHeight = 150;
	CGFloat margin = 10.0;
	CGFloat y = [UIApplication sharedApplication].statusBarFrame.size.height + margin;
	
	self.subview.frame = CGRectMake(margin, y, self.bounds.size.width - margin * 2.0, subviewHeight);
	self.subviewSwitch.frame = CGRectMake(self.subview.bounds.size.width - self.subviewSwitch.bounds.size.width - margin, margin, self.subviewSwitch.bounds.size.width, self.subviewSwitch.bounds.size.height);
	self.subviewButton.frame = CGRectMake(self.subview.bounds.size.width - 60.0 - margin, self.subview.bounds.size.height - margin - 30.0, 60.0, 30.0);
	
	y = CGRectGetMaxY(self.subview.frame) + margin;
	CGFloat controlsSpace = self.bounds.size.height - y - detailsButtonHeight;
	if (self.controls.count) {
		CGFloat controlHeight = (controlsSpace - margin * (self.controls.count - 1)) / self.controls.count;
		
		for (UIControl *control in self.controls) {
			control.frame = CGRectMake(margin, y, self.bounds.size.width - margin * 2.0, controlHeight);
			y = CGRectGetMaxY(control.frame) + margin;
		}
	}

//	self.detailsButton.frame = CGRectMake(margin, self.bounds.size.height - detailsButtonHeight, self.bounds.size.width - margin * 0.5, detailsButtonHeight);
}

@end

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com 
